//
//  DiscussSearchViewController.m
//  OperationPro
//
//  Created by yaxue wang on 16/10/31.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DiscussSearchViewController.h"
#import "DiscussView.h"
#import "DiscussModel.h"
#import "CreateDiscussViewController.h"
#import "DiscussDetailViewController.h"
#import "DiscussModel.h"
#import "InvitationModel.h"
@interface DiscussSearchViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) DiscussView *mainView;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIView *typeView;
@property (nonatomic, strong) NSString *kindStr;
@property (nonatomic, strong) TPKeyboardAvoidingScrollView *scrollView;

@end

@implementation DiscussSearchViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return  UIStatusBarStyleDefault;
}
- (void)initView
{
    [super initView];
    [self.navBar setHidden:YES];
    self.navBar.leftView = nil;
    self.navBar.bgColor =[UIColor clearColor];
    _kindStr=@"";
    UIView *searchView=[[UIView alloc]initWithFrame:CGRectMake(15, 25, WindowWidth-75, 30)];
    searchView.backgroundColor=[UIColor whiteColor];//C(@"eaeaea");
    searchView.layer.masksToBounds=YES;
    searchView.layer.cornerRadius=4;
    searchView.layer.borderColor=LineColor.CGColor;
    searchView.layer.borderWidth=0.5;
    [self.view addSubview:searchView];
    _searchField = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, searchView.bounds.size.width-10, 30)];
    _searchField.delegate = self;
    _searchField.placeholder = @"搜索";
    _searchField.font=FF(15);
    _searchField.returnKeyType=UIReturnKeyDone;
    _searchField.clearButtonMode=UITextFieldViewModeAlways;
    [searchView addSubview:_searchField];
  
    
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(WindowWidth-50, 25, 40, 30);
    [backBtn setTitle:@"取消" forState:UIControlStateNormal];
    [backBtn setTitleColor:MainColor forState:UIControlStateNormal];
    backBtn.titleLabel.font=FF(16);
    [self.view addSubview:backBtn];
    [backBtn bk_addEventHandler:^(id sender) {
        
        [self backController];
        
    } forControlEvents:UIControlEventTouchUpInside];
   
    self.scrollView =[[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 60, WindowWidth, self.view.bounds.size.height-60)];
    self.scrollView.contentSize=CGSizeMake(WindowWidth*2, self.view.bounds.size.height-CGRectGetMaxY(_searchField.frame));
    self.scrollView.scrollEnabled=NO;
    self.scrollView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:self.scrollView];
    
    __block DiscussSearchViewController *weakview = self;
    _mainView = [[DiscussView alloc] initWithFrame:CGRectMake(WindowWidth,0, WindowWidth, self.scrollView.bounds.size.height)];
    _mainView.tag=1000;
    __block typeof (self)blockSelf=self;

    [_mainView setScrollView:^(CGFloat height) {
        [blockSelf.searchField resignFirstResponder];
        [blockSelf.view endEditing:YES];
    }];
   [self.scrollView addSubview:_mainView];

    __block typeof(self) blockself = self;
    [_mainView setCCellSelect:^(DiscussModel *model) {
        DiscussDetailViewController *disscussVC=[[DiscussDetailViewController alloc]init];
        NSString *warn =model.D_warning_uuid;
        if (warn==nil) {
#pragma mark  贴子的详情
            [blockself setIsLoading:YES];
            [CClass HttpRequestTask:YES withUrl:@"discuss_detail" withData:@{@"uuid":model.D_uuid} notifyError:YES onSuccess:^(BaseModel *model) {
                [blockself setIsLoading:NO];
                
                disscussVC.invitationModel=[[InvitationModel alloc] initWithDictionary:model.B_data error:nil];
                disscussVC.isWarnuiud=NO;
                [weakview pushController:disscussVC];
                
            } onError:^(BaseModel *model) {
                [blockself setIsLoading:NO];

            }];
            
        }else
        {
#pragma mark  贴子的详情
            [blockself setIsLoading:YES];
            [CClass HttpRequestTask:YES withUrl:@"discuss_detail" withData:@{@"uuid":model.D_uuid} notifyError:YES onSuccess:^(BaseModel *model) {
#pragma mark  保修单的详情
                disscussVC.invitationModel=[[InvitationModel alloc] initWithDictionary:model.B_data error:nil];
                [CClass HttpRequestTask:YES withUrl:@"warninglist_orderContent" withData:@{@"warningUuid":warn} notifyError:YES onSuccess:^(BaseModel *model) {
                    [blockself setIsLoading:NO];
                    
                    disscussVC.imageString =[model.B_data objectForKey:@"image"];
                    disscussVC.describe =[model.B_data objectForKey:@"describe"];
                    disscussVC.warnuiud =warn;
                    disscussVC.isWarnuiud=YES;
                    [weakview pushController:disscussVC];
                    
                } onError:^(BaseModel *model) {
                    [blockself setIsLoading:NO];

                }];
                
                
            } onError:^(BaseModel *model) {
                [blockself setIsLoading:NO];

            }];
            
        }
        
    }];
    [_mainView setDiscussBecomeFirstResponse:^{
    }];
    
    //类型的图片
    _typeView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, self.scrollView.bounds.size.height)];
    [self.scrollView addSubview:_typeView];
    _typeView.tag=2000;
    UIImage *typeImg =[UIImage imageNamed:@"Ico_discuss_search1"];
    
    CGFloat space = (WindowWidth -40*3)/4;
    NSArray *ar=[[NSArray alloc]initWithObjects:@"提问",@"讨论",@"经验", nil];
    for (int i=0; i<3; i++) {
        
        UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake(space+(space+40)*i,108, 40, typeImg.size.height+100)];
        [_typeView addSubview:bgView];
        bgView.tag=i;
        UIImageView* photoImg =[[UIImageView alloc]init];
        photoImg.frame =CGRectMake((40-typeImg.size.width)/2,0, typeImg.size.width, typeImg.size.height);
        photoImg.image=[UIImage imageNamed:[NSString stringWithFormat:@"Ico_discuss_search%d",i+1]];
        photoImg.userInteractionEnabled =YES;
        [bgView addSubview:photoImg];
        
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0,typeImg.size.height+20, 40, 20)];
        lab.text=[ar objectAtIndex:i];
        lab.font=F(15);
        lab.textAlignment=NSTextAlignmentCenter;
        lab.textColor=MainColor;
        [bgView addSubview:lab];
      
        [bgView bk_whenTapped:^{
            [self.searchField resignFirstResponder];
            [self.view endEditing:YES];
            if (bgView.tag ==0){
                self.kindStr=@"01";
            }else  if (bgView.tag ==1) {
                self.kindStr=@"02";

            }else if (bgView.tag ==2)
            {
                self.kindStr=@"03";

            }
            self.scrollView.contentOffset=CGPointMake(WindowWidth, 0);
            NSArray *ar=[[NSArray alloc]initWithObjects:@"提问",@"讨论",@"经验", nil];
            NSInteger num=[self.kindStr integerValue];
            _searchField.placeholder = [NSString stringWithFormat:@"搜索%@",[ar objectAtIndex:num-1]];
            [self.mainView setWithKeyword:@"" kind:_kindStr];


        }];
    }



}
#pragma mark - UITextFieldDelegate
//开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([_kindStr isEqualToString:@""]&&[textField.text isEqualToString:@""]) {
        self.scrollView.contentOffset=CGPointMake(0, 0);
        
    }else
    {
        self.scrollView.contentOffset=CGPointMake(WindowWidth, 0);
        [self.mainView setWithKeyword:textField.text kind:_kindStr];
        
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//    UITextPosition *position = [textField positionFromPosition:[textField markedTextRange] offset:0];
//    UITextRange *r = [textField markedTextRange];
//    if (position) {
//        return YES;
//    }
////    if (textField.markedTextRange) {
////        return YES;
////    }
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    
 
    self.scrollView.contentOffset=CGPointMake(WindowWidth, 0);
    // 获得需要进行搜索的关键字
    NSString *shouldBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([_kindStr isEqualToString:@""]) {
        
        if([shouldBeString isEqualToString:@""])
        {
            self.scrollView.contentOffset=CGPointMake(0, 0);

            
        }else{
            
            [self.mainView setWithKeyword:shouldBeString kind:@""];

        }

    }else
    {
        // 搜索关联词汇
        [self.mainView.mainData removeAllObjects];
        [self.mainView reloadData];
        [self.mainView setWithKeyword:shouldBeString kind:_kindStr];
        
    }
    
    return YES;

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    // 搜索关联词汇
    if ([_kindStr isEqualToString:@""]) {
        [self.mainView setWithKeyword:textField.text kind:@""];
        
        
    }else
    {
        // 搜索关联词汇
        [self.mainView.mainData removeAllObjects];
        [self.mainView reloadData];
        [self.mainView setWithKeyword:textField.text kind:_kindStr];
        
    }

    return YES;
}
//clear
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if ([_kindStr isEqualToString:@""]) {
        self.scrollView.contentOffset=CGPointMake(0, 0);
        
    }else
    {
        self.scrollView.contentOffset=CGPointMake(WindowWidth, 0);
        [self.mainView setWithKeyword:@"" kind:_kindStr];

    }
    return YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchField resignFirstResponder];
    [self.view endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
